//
//  ViewController.m
//  LightMeter
//
//  Created by alex tao on 4/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RoomViewController.h"
#import "AVCamViewController.h"

@interface RoomViewController ()

@end

@implementation RoomViewController

@synthesize scrollView = _scrollView;
@synthesize areaText = _areaText;


- (void) internalInit
{
    _roomArr = [[NSArray alloc] initWithObjects:@"起居室", @"餐厅", @"书房", @"卧室", @"厨房", @"卫生间", @"儿童房", @"玄关", @"家庭影音室", @"衣帽间", @"储藏间", @"其他", nil];
    
    _keyboardShown = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self internalInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom initialization
        [self internalInit];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_roomArr release];
    [_scrollView release];
    [_areaText release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setScrollView:nil];
    [self setAreaText:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)done {
    [self.areaText resignFirstResponder];
    AVCamViewController * avVC = [[AVCamViewController alloc] initWithNibName:@"AVCamViewController" bundle:nil];
    avVC.roomName = [_roomArr objectAtIndex:_curIdx];
    [self.navigationController pushViewController:avVC animated:YES];
    [avVC release];
}

- (void)showHideKeyboard:(BOOL)show
{
    if (show != _keyboardShown){
        _keyboardShown = show;
        if (show){
            CGRect rcScrollView = _scrollView.frame;
            rcScrollView.size.height -= 200;
            _scrollView.frame = rcScrollView;
            [_scrollView setContentOffset:CGPointMake(0, 200) animated:YES];
        }else{
            CGRect rcScrollView = _scrollView.frame;
            rcScrollView.size.height += 200;
            _scrollView.frame = rcScrollView;
            [_scrollView scrollRectToVisible:CGRectZero animated:YES];
        }
    }
}


#pragma mark - UIKeyboard Notifications

- (void)keyboardDidShow:(NSNotification *)notification
{
    [self showHideKeyboard:YES];
}

- (void)keyboardDidHide:(NSNotification *)notification
{
    [self showHideKeyboard:NO];
}


#pragma mark - UIPickerViewDataSource, UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_roomArr count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_roomArr objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _curIdx = row;
}

@end
